import ajax, { AxiosResponse } from './ajax';

export enum DataStatus {
    sucess,
    failure
}

const [BASE, WEATHER] = ['/api1', '/api2'] as const;

// 请求登录验证
type ReqLogin = (username: string, password: string) => Promise<AxiosResponse>;

export const reqLogin: ReqLogin = (username, password) => ajax(`${BASE}/login`, { username, password }, 'POST');

// 请求添加用户
type UserInfoYes = 'username' | 'password';
type UserInfoNo = 'phone' | 'email' | 'role_id';
type UserConfig = Record<UserInfoYes, string> & Partial<Record<UserInfoNo, string>>;
type ReqAddUser = (user: UserConfig) => Promise<AxiosResponse>;

export const reqAddUser: ReqAddUser = user => ajax(`${BASE}/mangeage/user/add`, user, 'POST');

// 请求天气
type City = {
    weatherinfo: {
        city: string;
        weather: string;
        img2: string;
        [propName: string]: string;
    };
};
export type CityInfo = City['weatherinfo'];
type Weather = (CityId?: string) => Promise<CityInfo>;

export const reqWeather: Weather = (cityId = '101281205') => ajax(`${WEATHER}/data/cityinfo/${cityId}.html`).then(({ data }: AxiosResponse<City>) => data.weatherinfo);

// 获取一级/二级分类的列表
type ReqCategory = (parentId?: string) => Promise<AxiosResponse>;

export const reqCategory: ReqCategory = (parentId = '0') => ajax(`${BASE}/manage/category/list`, { parentId });

// 根据ID获取一个分类
type ReqIdToCategory = (categoryId: string) => Promise<AxiosResponse>;

export const reqIdToCategory: ReqIdToCategory = categoryId => ajax(`${BASE}/manage/category/info`, { categoryId });

// 添加分类
type ReqAddCategory = (categoryName: string, parentId?: string) => Promise<AxiosResponse>;

export const reqAddCategory: ReqAddCategory = (categoryName, parentId = '0') => ajax(`${BASE}/manage/category/add`, { categoryName, parentId }, 'POST');

// 更新分类
type UpdateCategory = {
    categoryId: string;
    categoryName: string;
};
type ReqUpdateCategory = (update: UpdateCategory) => Promise<AxiosResponse>;

export const reqUpdateCategory: ReqUpdateCategory = ({ categoryId, categoryName }) => ajax(`${BASE}/manage/category/update`, { categoryName, categoryId }, 'POST');

// 获取商品分类列表
type ReqProducts = (pageNum: number, pageSize?: number) => Promise<AxiosResponse>;

export const reqProducts: ReqProducts = (pageNum, pageSize) => ajax(`${BASE}/manage/product/list`, { pageNum, pageSize });

// 搜索商品分页列表（根据商品名称/商品描述）
type PageConfig = {
    pageNum: number;
    pageSize: number;
    searchName: string;
};
type ReqSearchProducts = (pageConfig: PageConfig, flag?: boolean) => Promise<AxiosResponse>;

export const reqSearchProducts: ReqSearchProducts = ({ pageNum, pageSize, searchName }, flag = false) => ajax(`${BASE}/manage/product/search`, { pageNum, pageSize, [`product${flag ? 'Desc' : 'Name'}`]: searchName });

//  更新商品的状态（在售/下架）
export enum Sold {
    Yes = 1,
    No
}
type ReqUpdateProductStatus = (productId: string, status: Sold) => Promise<AxiosResponse>;

export const reqUpdateProductStatus: ReqUpdateProductStatus = (productId, status) => ajax(`${BASE}/manage/product/updateStatus`, { status, productId }, 'POST');

// 请求删除图片
type ReqDeleteImg = (name: string) => Promise<AxiosResponse>;

export const reqDeleteImg: ReqDeleteImg = name => ajax(`${BASE}/manage/img/delete`, { name }, 'POST');

// 请求添加/修改商品
type ReqAddUpdateProduct = (product: any) => Promise<AxiosResponse>;

export const reqAddUpdateProduct: ReqAddUpdateProduct = product => ajax(`${BASE}/manage/product/${product._id ? 'update' : 'add'}`, product, 'POST');

// 请求所有角色列表
type ReqRoles = () => Promise<AxiosResponse>;

export const reqRoles: ReqRoles = () => ajax(`${BASE}/manage/role/list`);

// 添加角色
type ReqAddRole = (roleName: string) => Promise<AxiosResponse>;

export const reqAddRole: ReqAddRole = roleName => ajax(`${BASE}/manage/role/add`, { roleName }, 'POST');

// 更新角色
type ReqUpdateRole = (role: any) => Promise<AxiosResponse>;

export const reqUpdateRole: ReqUpdateRole = role => ajax(`${BASE}/manage/role/update`, role, 'POST');

// 获取所有用户列表
type ReqUsers = () => Promise<AxiosResponse>;

export const reqUsers: ReqUsers = () => ajax(`${BASE}/manage/user/list`);

// 删除用户
type ReqDeleteUser = (userId: string) => Promise<AxiosResponse>;

export const reqDeleteUser: ReqDeleteUser = userId => ajax(`${BASE}/manage/user/delete`, { userId }, 'POST');

// 添加用户
type ReqAddUserInfo = (user: Record<string, string>) => Promise<AxiosResponse>;

export const reqAddUserInfo: ReqAddUserInfo = user => ajax(`${BASE}/manage/user/add`, user, 'POST');

// 更新用户
export const reqUpdateUserInfo: ReqAddUserInfo = user => ajax(`${BASE}/manage/user/update`, user, 'POST');

export type { AxiosResponse };